package com.jiaquyun.jcyx.main

import android.content.Intent
import android.net.Uri
import com.jiaquyun.jcyx.R
import com.jiaquyun.jcyx.app.JCApplication
import com.jiaquyun.jcyx.entity.UserShops
import com.jiaquyun.jcyx.jetpack.mode.MineViewModel
import com.jiaquyun.jcyx.main.mine.*
import com.jiaquyun.jcyx.popup.MineServicePopup
import com.jiaquyun.jcyx.utils.SpanUtils
import com.module.lemlin.http.Status
import com.module.lemlin.image.loadCenterImage
import com.module.lemlin.image.loadCircleImage
import com.module.lemlin.image.loadRoundImage
import com.module.lemlin.owner.OwnerViewModeFragment
import kotlinx.android.synthetic.main.fragment_main_mine.*
import java.lang.StringBuilder

class MainMineFragment : OwnerViewModeFragment<MineViewModel>() {

    companion object {
        fun newInstance(): MainMineFragment {
            return MainMineFragment()
        }
    }

    private val mMineServicePopup: MineServicePopup by lazy {
        MineServicePopup(context = requireContext()) {
            val intent = Intent(Intent.ACTION_DIAL)
            val data: Uri = Uri.parse("tel:13088729727")
            intent.data = data
            startActivity(intent)
        }
    }

    override val layoutResId: Int
        get() = R.layout.fragment_main_mine

    override fun viewModel(): MineViewModel {
        return createViewModel()
    }

    override fun initView() {
        JCApplication.userInfo?.avatar?.let {
            ivMainMineUserAvatar.loadCircleImage(it)
        }
        tvMainMineUserName.text =
            JCApplication.userInfo?.nickname

        //收货地址
        tvMainMineAddress.setOnClickListener {
            MineAddressActivity.open(activity = requireActivity())
        }
        //我的收藏
        tvMainMineCollect.setOnClickListener {
            MineCollectActivity.open()
        }
        //关于我们
        tvMainMineAbout.setOnClickListener {
            MineRichTextActivity.openId(activity = requireActivity(), title = "关于我们", id = 2)
        }
        //常购清单
        tvMainMineInvent.setOnClickListener {
            MinePurchaseActivity.open()
        }
        //联系客服
        tvMainMineService.setOnClickListener {
            mMineServicePopup.showPopupWindow()
        }
        //帮助与反馈
        tvMainMineHelp.setOnClickListener {
            MineHelpActivity.open()
        }
        //消息
        tvMainMineMessage.setOnClickListener {
            MineMessageActivity.open()
        }
        //设置
        tvMainMineSetting.setOnClickListener {
            MineSettingActivity.open()
        }

        //优惠券
        llMainMineCoupon.setOnClickListener {
            MineCouponActivity.open()
        }

        //余额
        llMainMineMoney.setOnClickListener {
            MineMoneyActivity.open()
        }

        llMainMineScore.setOnClickListener {
            MineIntegralActivity.open()
        }

        linearLayout2.setOnClickListener {
            MineOrderActivity.open()
        }

        //邀请有礼
//        tvMainMineInvite.setOnClickListener {
//            MineShareActivity.open()
//        }

        //开具发票
        tvMainMineInvoice.setOnClickListener {
            MineInvoiceActivity.open()
        }

        //佣金
        tvMainMineCommission.setOnClickListener {
            MineYongActivity.open()
        }
    }

    override fun initData() {
        viewModel.userIndex()
    }

    override fun onRefreshLoad() {
        viewModel.userIndex()
    }

    override fun initViewMode() {
        viewModel.userIndexLiveDate.observe(viewLifecycleOwner, { response ->
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe

            ivMainMineUserAvatar.loadCircleImage(
                data.userInfo.avatar
            )
            tvMainMineUserName.text =
                data.userInfo.nickname
            tvMainMineCoupon.text =
                data.coupon_count.toString()
            tvMainMineMoney.text =
                SpanUtils.setMoneySymbol(data.userInfo.money)
            tvMainMineScore.text =
                data.userInfo.score.toString()
        })
    }

    override fun onEventMainThread(any: Any) {
        if (any is UserShops) {
            initData()
        }
    }

}